<?php

namespace Server\Controller;
use Think\Controller\HproseController;
class UserController extends HproseController {
	public function bind($id,$pwd,$wgateid){
		header("Content-Type:text/html; charset = utf-8");
		if(strlen($id) != 8){
			$result['success'] = 0;
			$result['info'] = '错误参数';
			return json_encode($result );
		}
		// $output = linkurl($url,'',3);
		$url = "http://services.shu.edu.cn/Login.aspx";
		$info=file_get_contents($url);
		preg_match('<input type="hidden" name="__VIEWSTATE" value="(.*)" />',$info,$m);
		$post_data = array ("__EVENTTARGET" => "","__EVENTARGUMENT"=>"","__VIEWSTATE"=>$m[1],"txtUserName"=>$id,"txtPassword"=>$pwd,"btnOk"=>"提交(Submit)");
        //$output = substr(linkurl($url,$post_data,1),0,30);
        $output = linkurl($url,$post_data,1);
		if(strpos($output,'Object move')){
			$r = M('user')->where(array('del'=>0,'wgateid'=>$wgateid))->find();
			if(count($r) == 0) {
				$res = R('Weixin/getUserInfo', array($wgateid));
				$data['wgateid'] = $wgateid;
				$data['name'] = $res->nickname;
				$data['photo'] = $res->headimgurl;

				$data['del'] = 0;
				$data['sid'] = $id;
				$data['createtime'] = date("Y-m-d H:i:s");
				if(preg_match ("/^1[0-9]12/", $id))
					$data['type'] = 1;
				else
					$data['type'] = 2;
				$uid = M('user')->add($data);
				$data1['id'] = $id;
				$data1['password'] = base64_encode($pwd);
				$data1['user_id'] = $uid;
				if($data['type'] == 1)
					M('student')->add($data1);
				else
					M('teacher')->add($data1);
				$result['success'] = 1;
				setcookie('shu_ces_wechat_uid',base64_encode($uid),0,'/');
				setcookie('shu_ces_wechat_sid',base64_encode($id),0,'/');
			}
			else{
				$result['success'] = 2;
				$result['uid'] = $r['id'];
				$result['sid'] = $id;

			}
		}
		else{
			$result['success'] = 0;
			$result['info'] = '账号或密码错误';
		}
		return json_encode($result);
	}

	public function getBindState($wgateid){
		header("Content-Type:text/html; charset = utf-8");
		$r = M('user')->where(array('del'=>0,'wgateid'=>$wgateid))->find();
		if(count($r) != 0){
			switch ($r['state']){
				case '2':$result['state'] = 2;break;
				case '3':$result['state'] = 3;break;
			}
		}
		else{
			$result['state'] = 0;
		}
		$result['success'] = 1;
		return json_encode($result );

	}

	public function getIniPersonInfo($sid){
		header("Content-Type:text/html; charset = utf-8");
		$type = M('user')->where(array('del'=>0,'sid'=>$sid))->getField('type');
		if($type == '1')
			$result['result'] = M('student')->where(array('del'=>0,'id'=>$sid))->find();
		else
			$result['result'] = M('teacher')->where(array('del'=>0,'id'=>$sid))->find();
		$result['success'] = 1;
		return json_encode($result );

	}

	public function iniPersonInfo($sid,$name,$sex,$birthday,$phone){
		header("Content-Type:text/html; charset = utf-8");

		$data['id'] = $sid;
		$data['name'] = $name;
		$data['sex'] = $sex;
		$data['birthday'] = $birthday;
		$data['phone'] = $phone;
		$data['state'] = 2;
		$type = M('user')->where(array('del'=>0,'sid'=>$sid))->getField('type');
		if($type == '1'){
				M('student')->save($data);
				M('user')->where(array('del'=>0,'sid'=>$sid))->setField('state',2);
		}

		else{
				M('teacher')->save($data);
				M('user')->where(array('del'=>0,'sid'=>$sid))->setField('state',2);
		}
		$result['wgateid'] = M('user')->where(array('del'=>0,'sid'=>$sid))->getField('wgateid');
		$result['success'] = 1;
		return json_encode($result );
	}

	public function getUserInfo($wgateid)
	{
			header("Content-type: text/html; charset=utf-8");
			$res = M('user')->where(array('del'=>0,'wgateid'=>$wgateid))->find();
			if(count($res) == 0)
				$result['success'] = 0;
			else {
				if($res['type'] == '1'){
					$info = M('student')->where(array('del'=>0,'id'=>$res['sid']))->find();
				}
				else{
					$info = M('teacher')->where(array('del'=>0,'id'=>$res['sid']))->find();
				}
				if($info['state'] == '1')
					$result['success'] = 2;
				else if($info['state'] == '2') {
					$result['success'] = 1;
				}
				else{
					$result['success'] = 3;
				}
				$result['info']=$info;
				$result['uid'] = $res['id'];
				$result['type'] = $res['type'];
				return json_encode($result );
			}
	}
}
